package de.tum.in.eist.poll.client;

import de.tum.in.eist.poll.shared.Lecturer;
import de.tum.in.eist.poll.shared.Student;
import java.util.Date;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;

/***
 * Main Page EntryPoint
 */
public class Poll implements EntryPoint, ValueChangeHandler<String>{
	// Setting up Communication
	final static PollServiceAsync pollService = GWT.create(PollService.class);
	final static AdminServiceAsync adminService = GWT.create(AdminService.class);
	final static AuthServiceAsync authService = GWT.create(AuthService.class);
	final static StatServiceAsync statService = GWT.create(StatService.class);

	// Menu on top-right
	private final MenuWidget menu = new MenuWidget();

	// State-tracking values
	static Student student;
	static Lecturer lecturer;
	static Date currentLecDate;
	
	/***
	 * starting point
	 */
	public void onModuleLoad(){
		String initToken = History.getToken();
	    if (initToken.length() == 0){
	    	History.newItem("studentLogin");
	    }
	    // Add history listener
	    History.addValueChangeHandler(this);
	    // Now that we've setup our listener, fire the initial history state.
	    History.fireCurrentHistoryState();
	}
	/***
	 * fired on changing of History state
	 * @param event
	 */
	@Override
	public void onValueChange(ValueChangeEvent<String> event){
		RootPanel.get("container").clear();
		if(event.getValue().equals("studentLogin")){
			menu.initMode("student");
			StudentLoginView.paint();
		}else if(event.getValue().equals("studentSurvey")){
			menu.initMode("student");
			StudentSurveyView.paint();
		}else if(event.getValue().equals("viewResults")){
			menu.initLecturerMenu(event.getValue());
			ResultsView.paint();
		}else if(event.getValue().equals("lecturerLogin")){
			menu.initMode("lecturer");
			LecturerLoginView.paint();
		}else if(event.getValue().equals("manageIDs")){
			menu.initLecturerMenu(event.getValue());
			ManageIDsView.paint();
		}else if(event.getValue().equals("manageLectures")){
			menu.initLecturerMenu(event.getValue());
			ManageLecturesView.paint();
		}
	}	
}
